package com.yyfax.ph.entrance;

import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import cn.jpush.android.api.JPushInterface;
import com.umeng.analytics.MobclickAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.ph.BuildConfig;
import com.yyfax.ph.R;
import com.yyfax.ph.base.PermissionActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.common.utils.preferenceutils.PreferenceConstants;
import com.yyfax.ph.main.MainActivity;

/**
 * 启动页
 * 加载启动页图片然后动画且2秒后跳转
 */
public class SplashActivity extends PermissionActivity{
	private static final String TAG = "SplashActivity";

	private int mPermissionRequestCodePhoneStatusExternalStorage = DEFAULT_PERMISSION_REQUEST_CODE_PHONE_STATUS_EXTERNAL_STORAGE;

	@Override
	public int getContentViewRes() {
		return R.layout.splash_layout_activity;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopBarVisible(false);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// 取缓存的versionCode
		int cacheVersion = YJPH.getInstance().getPreferenceManager().getAppVersion(0);
		// 取当前app的versionCode
		int appVersion = BuildConfig.VERSION_CODE;
		// getPackageName()是你当前类的包名，0代表是获取版本信息
		// 二者作比较，如果当前app的versionCode大于缓存的，则说明是升级安装，退出登录，并且缓存新的版本号
		if (appVersion > cacheVersion && cacheVersion > 0) {
			YJPH.getInstance().onLogoutSuccess();
			YJPH.getInstance().getPreferenceManager().savePreference(PreferenceConstants.SP_KEY_APP_VERSION, appVersion);
			JPushInterface.stopPush(YJPH.getInstance());
		}

		//for umeng.com analytics sdk
		if (getPhoneStautsExternalStoragePermission(mPermissionRequestCodePhoneStatusExternalStorage)) {
			startNextStep();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("SplashActivity");
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("SplashActivity");
		MobclickAgent.onPause(this);
	}

	private void initMobclickAgent() {
		MobclickAgent.setDebugMode(true);
		MobclickAgent.openActivityDurationTrack(false);
		MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
		MobclickAgent.onEvent(this, "initMobclickAgent");
	}

	@Override
	protected void permissionsAllGrantedSuccess(int requestCode) {
		if (requestCode == mPermissionRequestCodePhoneStatusExternalStorage) {
			startNextStep();
		}
	}

	@Override
	public void permissionsAllGrantedFailure(int requestCode) {
		if (requestCode == mPermissionRequestCodePhoneStatusExternalStorage) {
			finish();
		}
	}

	private void startNextStep(){
		initMobclickAgent();
		startAnimation();
	}

	private void startAnimation() {
		AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f);
		aa.setDuration(2000);
		aa.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
				boolean firstFlag = YJPH.getInstance().getPreferenceManager().isFirstStartApp(true);

				if (firstFlag) {
					GuideActivity.start(SplashActivity.this);
					finish();
				} else {
					MainActivity.start(SplashActivity.this);
					finish();
				}
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationStart(Animation animation) {
			}
		});
		View v = findViewById(R.id.splash_container);
		v.startAnimation(aa);
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}
}
